local panshi = fk.CreateSkill {

  name = "joy__panshi",

  tags = { Skill.Compulsory, },

}



panshi:addEffect(fk.EventPhaseStart, {
  name = "joy__panshi",
  events = {fk.EventPhaseStart, fk.DamageCaused, fk.Damage},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(panshi.name) and player == target then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Start and table.find(player.room.alive_players, function (p)
          return p ~= player and p:hasSkill(cixiao.name, true) end)
      elseif event == fk.DamageCaused or event == fk.Damage then
        return player.phase == Player.Play and data.to:hasSkill(cixiao.name, true) and
        data.card and data.card.trueName =="slash" and not data.chain
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, panshi.name, "negative")
      player:broadcastSkillInvoke(panshi.name)
      local fathers = table.filter(room.alive_players, function (p) return p ~= player and p:hasSkill(cixiao.name, true) end)
      if #fathers == 1 then
        room:doIndicate(player.id, {fathers[1].id})
        if player:isKongcheng() then return false end
        local card = room:askForCard(player, 1, 1, false, panshi.name, false, ".", "#panshi-give-to:"..fathers[1].id)
        if #card > 0 then
          room:obtainCard(fathers[1].id, card[1], false, fk.ReasonGive)
        end
      else
        local tos, id = room:askForChooseCardAndPlayers(player, table.map(fathers, Util.IdMapper), 1, 1, ".|.|.|hand", "#panshi-give", panshi.name, false)
        if #tos > 0 and id then
          room:obtainCard(tos[1], id, false, fk.ReasonGive)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, panshi.name, "offensive")
      player:broadcastSkillInvoke(panshi.name)
      data.damage = data.damage + 1
    elseif event == fk.Damage then
      room:notifySkillInvoked(player, panshi.name, "negative")
      player:broadcastSkillInvoke(panshi.name)
      room:handleAddLoseSkills(player,"-"..panshi.name)
      player:endPlayPhase()
    end
  end,
})
panshi:addEffect(fk.DamageCaused, {
  name = "joy__panshi",
  events = {fk.EventPhaseStart, fk.DamageCaused, fk.Damage},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(panshi.name) and player == target then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Start and table.find(player.room.alive_players, function (p)
          return p ~= player and p:hasSkill(cixiao.name, true) end)
      elseif event == fk.DamageCaused or event == fk.Damage then
        return player.phase == Player.Play and data.to:hasSkill(cixiao.name, true) and
        data.card and data.card.trueName =="slash" and not data.chain
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, panshi.name, "negative")
      player:broadcastSkillInvoke(panshi.name)
      local fathers = table.filter(room.alive_players, function (p) return p ~= player and p:hasSkill(cixiao.name, true) end)
      if #fathers == 1 then
        room:doIndicate(player.id, {fathers[1].id})
        if player:isKongcheng() then return false end
        local card = room:askForCard(player, 1, 1, false, panshi.name, false, ".", "#panshi-give-to:"..fathers[1].id)
        if #card > 0 then
          room:obtainCard(fathers[1].id, card[1], false, fk.ReasonGive)
        end
      else
        local tos, id = room:askForChooseCardAndPlayers(player, table.map(fathers, Util.IdMapper), 1, 1, ".|.|.|hand", "#panshi-give", panshi.name, false)
        if #tos > 0 and id then
          room:obtainCard(tos[1], id, false, fk.ReasonGive)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, panshi.name, "offensive")
      player:broadcastSkillInvoke(panshi.name)
      data.damage = data.damage + 1
    elseif event == fk.Damage then
      room:notifySkillInvoked(player, panshi.name, "negative")
      player:broadcastSkillInvoke(panshi.name)
      room:handleAddLoseSkills(player,"-"..panshi.name)
      player:endPlayPhase()
    end
  end,
})
panshi:addEffect(fk.Damage, {
  name = "joy__panshi",
  events = {fk.EventPhaseStart, fk.DamageCaused, fk.Damage},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(panshi.name) and player == target then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Start and table.find(player.room.alive_players, function (p)
          return p ~= player and p:hasSkill(cixiao.name, true) end)
      elseif event == fk.DamageCaused or event == fk.Damage then
        return player.phase == Player.Play and data.to:hasSkill(cixiao.name, true) and
        data.card and data.card.trueName =="slash" and not data.chain
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, panshi.name, "negative")
      player:broadcastSkillInvoke(panshi.name)
      local fathers = table.filter(room.alive_players, function (p) return p ~= player and p:hasSkill(cixiao.name, true) end)
      if #fathers == 1 then
        room:doIndicate(player.id, {fathers[1].id})
        if player:isKongcheng() then return false end
        local card = room:askForCard(player, 1, 1, false, panshi.name, false, ".", "#panshi-give-to:"..fathers[1].id)
        if #card > 0 then
          room:obtainCard(fathers[1].id, card[1], false, fk.ReasonGive)
        end
      else
        local tos, id = room:askForChooseCardAndPlayers(player, table.map(fathers, Util.IdMapper), 1, 1, ".|.|.|hand", "#panshi-give", panshi.name, false)
        if #tos > 0 and id then
          room:obtainCard(tos[1], id, false, fk.ReasonGive)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, panshi.name, "offensive")
      player:broadcastSkillInvoke(panshi.name)
      data.damage = data.damage + 1
    elseif event == fk.Damage then
      room:notifySkillInvoked(player, panshi.name, "negative")
      player:broadcastSkillInvoke(panshi.name)
      room:handleAddLoseSkills(player,"-"..panshi.name)
      player:endPlayPhase()
    end
  end,
})
panshi:addEffect(fk.EventLoseSkill, {
  refresh_events = {fk.EventLoseSkill, fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    return player == target and data == self
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@joy__panshi_son", event == fk.EventAcquireSkill and 1 or 0)
  end,
})
panshi:addEffect(fk.EventAcquireSkill, {
  refresh_events = {fk.EventLoseSkill, fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    return player == target and data == self
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@joy__panshi_son", event == fk.EventAcquireSkill and 1 or 0)
  end,
})

return panshi